<template>
  <div class="search">
    <!-- 顶部搜索 -->
    <div class="topBar">
      <div class="above">
        <div class="back" @click="back">
          <span class="iconfont icon-youjiantou1"></span>
        </div>

        <div class="input">
          <span class="fdj iconfont icon-Magnifier"></span>
          <input type="text" v-model="searchVal" />
        </div>
        <div class="searchBtn">
          <span> {{ searchVal ? "取消" : "搜索" }} </span>
        </div>
      </div>
    </div>

    <!-- 历史搜索 -->
    <div class="history">
      <div class="above">
        <span class="title">历史搜索</span>
        <span class="iconfont icon-shuaxin"></span>
      </div>
      <div class="content">
        <ul v-if="historyData">
          <li
            v-for="(book, index) in historyData"
            :key="index"
            @click="goSearch(book.bookName)"
          >
            <span>{{ book.bookName }}</span>
          </li>
        </ul>
      </div>
    </div>
    <!-- 搜索时进行结果弹窗 -->
    <div class="searchResult" v-if="searchVal !== ''">
      <ul>
        <li @click="goSearch(searchVal)" class="searchVal">
          <!-- <router-link
          tag="li"
          :to="{ name: 'searchall', query: { keywords: searchVal } }"
        > -->
          <div class="fdj">
            <span class="iconfont icon-Magnifier"></span>
          </div>
          <h3>搜索:</h3>
          <div>
            "<span class="text"> {{ searchVal }} </span>"
          </div>
        </li>
        <li
          v-for="result in ResultData"
          @click="goBookDetail(result.id, result.text)"
          :key="result.id"
        >
          <div class="fdj">
            <span class="iconfont icon-018shuben"></span>
          </div>
          <div class="text">
            <span>{{ result.text }}</span>
          </div>
        </li>
      </ul>
    </div>
    <div class="hot_search">
      <div class="title">热门搜索</div>
      <ul>
        <li
          @click="goBookDetail(hot.id, hot.name)"
          v-for="(hot, index) in hotSearch"
          :key="hot.id"
        >
          <i> {{ index + 1 }}</i> <span>{{ hot.name }}</span>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { getSearchResult, getHotSearch } from "../../api/search";

export default {
  data() {
    return {
      searchVal: "",
      ResultData: [],
      debGetRes: null,
      hotSearch: null,
      historyData: null,
    };
  },
  created() {
    this.getHotSearchFun();
    this.historyData = JSON.parse(window.localStorage.getItem("bookStorage"));
  },
  watch: {
    searchVal: {
      handler(val) {
        val && val ? this.debGetRes(val) : (this.ResultData = []);
        // 不行
        // 不行报错
        // val && val
        //   ? this._.debounce(this.getSearchResultFun(val), 500)
        //   : (this.ResultData = []);
        // 直接调用了
        // val && val
        //   ? this._.debounce(this.getSearchResultFun, 500)(val)
        //   : (this.ResultData = []);
      },
      deep: true,
    },
  },
  mounted() {
    this.debGetRes = this._.debounce(this.getSearchResultFun, 500);
  },
  methods: {
    getSearchResultFun(val) {
      getSearchResult({ search: val })
        .then(({ data: res }) => {
          this.ResultData = res.words.w;
        })
        .catch((err) => {
          if (err) {
            this.$toast({
              message: "服务器崩溃了",
              position: "middle",
            });
          }
        });
    },
    // 得到内容
    getHotSearchFun() {
      getHotSearch()
        .then(({ data: res }) => {
          this.hotSearch = res.list;
        })
        .catch((err) => {
          if (err) {
            this.$toast({
              message: "服务器崩溃了",
              position: "middle",
            });
          }
        });
    },
    setStorage(name) {
      //定义保存结构
      let obj = {
        bookName: name,
      };
      // 获取缓存中的内容
      let localStorage = JSON.parse(window.localStorage.getItem("bookStorage"));
      if (!localStorage) {
        window.localStorage.setItem("bookStorage", JSON.stringify([obj]));
      } else {
        // 查找是否存在
        let hasIndex = localStorage.findIndex((item) => item.bookName == name);
        // 如果存在打断保存操作
        if (hasIndex != -1) return;
        // 最大保存数量
        if (localStorage.length > 8) {
          localStorage.pop();
        }
        // 头部加入
        localStorage.unshift(obj);
        // 保存缓存
        window.localStorage.setItem(
          "bookStorage",
          JSON.stringify(localStorage)
        );
      }
    },
    goBookDetail(id, name) {
      this.setStorage(name);
      this.$router.push({
        name: "comment",
        query: { id: id, title: name },
      });
    },
    goSearch(searchVal) {
      this.setStorage(searchVal);
      this.$router.push({ name: "search", query: { search: searchVal } });
    },
    back() {
      this.$emit("change-search-show", false);
    },
  },
};
</script>

<style lang="less" scoped>
.search {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 950;
  background: #f7f7f7;
  overflow: scroll;
  .topBar {
    background-color: #fff;
    z-index: 960;
    .above {
      padding: 6px 3px;
      display: flex;
      align-items: center;
      .back {
        flex: 0 0 25px;
        text-align: center;
        line-height: 25px;
        .iconfont {
          width: 17px;
          height: 17px;
        }
      }
      .input {
        position: relative;
        flex: 1;
        .fdj {
          position: absolute;
          top: 4px;
          left: 10px;
        }
        input[type="text"] {
          height: 25px;
          width: 100%;
          border-radius: 12px;
          background-color: #f8f8f8;
          border: 0;
          padding-left: 30px;
          box-sizing: border-box;
        }
      }
      .searchBtn {
        span {
          color: #4c4c4c;
          font-size: 17px;
        }
      }
    }
    .module {
      display: flex;
      padding: 8px 0px;
      justify-content: space-around;
      .icon_block {
        text-align: center;
        width: 95px;

        span {
          font-size: 14px;
        }
        border-right: 2px solid #eeeeee;
        &:last-of-type {
          border-right: transparent;
        }
      }
    }
  }

  .history {
    // width: 350px;
    // margin: 10px auto 16px auto;
    margin-bottom: 10px;
    padding: 0 13px;

    .above {
      // font-weight: 500;
      display: flex;
      color: #adadad;
      font-size: 12px;
      // justify-content: space-between;
      .iconfont {
        color: #e1e1e1;
        font-size: 18px;
      }
    }
    .content {
      margin-top: 13px;
      overflow: hidden;
      ul {
        display: flex;
        margin-bottom: -50px; /* Maximum width of scrollbar */
        padding-bottom: 50px; /* Maximum width of scrollbar */
        overflow-x: scroll;
        // justify-content: space-around;
        li {
          margin-right: 12px;
          box-sizing: border-box;
          height: 23px;
          line-height: 11px;
          background: #fff;
          text-align: center;
          color: #adadad;
          border-radius: 12px;
          font-size: 12px;
          padding: 6px 3px;
          white-space: nowrap;
          text-overflow: ellipsis;
          overflow: hidden;
          word-break: break-all;
          min-width: 80px;
        }
      }
    }
  }

  .searchResult {
    background: #faf7f5;
    overflow: scroll;
    position: fixed;
    z-index: 970;
    top: 37px;
    left: 0;
    width: 100%;
    height: 100%;

    ul {
      font-size: 15px;
      padding: 0 15px;
      .iconfont {
        color: #dbd9d8;
        font-size: 13px;
        margin-right: 12px;
      }
      .searchVal {
        border-bottom: 2px solid #f8f8f8;
        display: flex;
        height: 40px;
        align-items: center;
        h3 {
          margin-right: 10px;
        }
        .text {
          color: #9fc9f3;
        }
      }
      li {
        height: 40px;
        display: flex;
        align-items: center;
        .fdj {
          flex: 0 0 20px;
          text-align: center;

          line-height: 20px;
        }
        .text {
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
          max-width: 200px;
          flex: 1;
          height: 52px;
          line-height: 52px;
          border-bottom: 2px solid #f8f8f8;
        }
      }
    }
  }
  .hot_search {
    padding: 0 13px;
    .title {
      color: #adadad;
      font-size: 12px;
    }
    ul {
      padding: 20px 15px;
      width: 100%;
      display: flex;
      flex-wrap: wrap;
      li {
        // flex: 1;
        width: 50%;
        margin-bottom: 17px;
        color: #7d7d7d;
        font-size: 13px;
        display: flex;
        i {
          display: block;
          width: 15px;
          height: 15px;
          border-radius: 50%;
          font-size: 12px;
          color: #fff;
          text-align: center;
          line-height: 15px;
          background: #d6d6d6;
        }
        span {
          margin-left: 12px;
        }
      }
      li:nth-of-type(1) {
        i {
          background: #e64d2e;
        }
      }
      li:nth-of-type(2) {
        i {
          background: #ff7919;
        }
      }
      li:nth-of-type(3) {
        i {
          background: #ffbf00;
        }
      }
    }
  }
}
</style>